/*
 *  others.cpp
 *  T3nsors2
 *
 *  Created by Michael Barriault on 10-06-17.
 *  Copyright 2010 University of Guelph. All rights reserved.
 *
 */

#include "others.h"
#include "macros.h"
#include <cstdlib>
#include <sys/time.h>
#include <iostream>
#include <sstream>

string itos(int i) {
	std::ostringstream str;
	str << i;
	return str.str();
}

int stoi(string str) {
	std::istringstream istr(str);
	int i;
	istr >> i;
	return i;
}

double stof(string str) {
	std::istringstream istr(str);
	double i;
	istr >> i;
	return i;
}

void Alloc_Error(string id) {
	std::cout << "Allocation error making " << id << std::endl;
}

void print(string str, bool fl) {
#ifdef DEBUG
	std::cout << str << std::flush;
	if ( not fl ) std::cout << std::endl;
#endif
}

string timer(struct timeval before, int mult) {
	std::ostringstream str;
	struct timezone tzp;
	struct timeval now,lapsed;
	gettimeofday(&now,&tzp);
	if ( before.tv_usec > now.tv_usec ) {
		now.tv_usec += 1000000;
		now.tv_sec--;
	}
	lapsed.tv_usec = now.tv_usec - before.tv_usec; 
	lapsed.tv_sec = now.tv_sec - before.tv_sec; 
	int left = int(mult*(lapsed.tv_sec+double(lapsed.tv_usec)/1E6));
	if ( left > 3600 )
		str << left/3600 << "h ";
	if ( left > 60 )
		str << (left%3600)/60 << "m ";
	str << left%60 << "s" << std::flush;
	return str.str();
}
